##### My (demuredemeanor) bashrc sub source function script
# Uses tabstop=4; shiftwidth=4 tabs; foldmarker={{{,}}};
# https://notabug.org/demure/dotfiles/
# legacy repo http://github.com/demure/dotfiles
# vim:set syntax=sh:

### Universal Custom Commands ### {{{
	### Extract Function ### {{{
	## Extract most types of compressed files
	function extract {
		echo Extracting $1 ...
		if [ -f $1 ] ; then
			case $1 in
				*.tar.bz2)	tar xjf $1		;;
				*.tar.gz)	tar xzf $1		;;
				*.bz2)		bunzip2 $1		;;
				*.rar)		rar x $1		;;
				*.gz) 		gunzip $1		;;
				*.tar)		tar xf $1		;;
				*.tbz2)		tar xjf $1		;;
				*.tgz)		tar xzf $1		;;
				*.zip)		unzip $1		;;
				*.Z)		uncompress $1	;;
				*.7z)		7z x $1			;;
				*)			echo "'$1' cannot be extracted via extract()" ;;
			esac
		  else
			echo "'$1' is not a valid file"
		fi
	}
	### End Extract ### }}}

	### Man Function ### {{{
	## Color man pages with less (is a `most` way too...)
	man() {
		env \
		LESS_TERMCAP_mb=$(printf "\e[1;31m") \
		LESS_TERMCAP_md=$(printf "\e[1;31m") \
		LESS_TERMCAP_me=$(printf "\e[0m") \
		LESS_TERMCAP_se=$(printf "\e[0m") \
		LESS_TERMCAP_so=$(printf "\e[1;44;33m") \
		LESS_TERMCAP_ue=$(printf "\e[0m") \
		LESS_TERMCAP_us=$(printf "\e[1;32m") \
		man "$@"
	}
	### End Man ### }}}

	### Tmux Display Funtion ### {{{
	## For resyncing $DISPLAY
	# used to refresh ssh connection for tmux
	# http://justinchouinard.com/blog/2010/04/10/fix-stale-ssh-environment-variables-in-gnu-screen-and-tmux
	function r() {
		if [[ -n $TMUX ]]; then
			NEW_DISPLAY=`tmux showenv|grep ^DISPLAY|cut -d = -f 2`
			if [[ -n $NEW_DISPLAY ]] && [[ -S $NEW_DISPLAY ]]; then
				DISPLAY=$NEW_DISPLAY
			fi
		fi
	}
	### End Tmux Display ### }}}

	### Git Files Hist Function ### {{{
	function git_files_hist() {
	git log --pretty=%h | while read hash; do git ls-tree -r $hash | awk '{print $4}'; done | sort -u
	}
	### End Git Hist ### }}}

	### Backup File ### {{{
	# http://xmodulo.com/useful-bash-aliases-functions.html
	backup() { cp "$1"{,.bak};}
	### End Backup ### }}}

	### IP Look Up ### {{{
	ipinfo() { 
		local IP1 IP2
		IP1="$1"
		IP2="$(echo "${IP1}" | grep -Ec '^(([0-9a-f]{0,4}:){1,7}[0-9a-f]{1,4}|([0-9]{1,3}\.){3}[0-9]{1,3})$')"
		if [[ ${IP2} == "1" ]]; then
			curl ipinfo.io/"$IP1"
		  else
			ipawk=($(host "$IP1" | awk '/address/ { print $NF }'))
			curl ipinfo.io/${ipawk[1]}
		fi
		echo
	}
	### End IP Look Up ### }}}
###End Universal Commands ### }}}
